package blog.service.impl;

import blog.mapper.BlogMapper;
import blog.mapper.CommentMapper;
import blog.pojo.Blog;
import blog.pojo.Comments;
import blog.pojo.ResultVo;
import blog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author HuLin
 * @version 1.0
 * @date 2022/9/16 10:03
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private BlogMapper blogMapper;

    @Override
    public ResultVo addComments(Comments comments) {
        comments.setCreateTime(new Date());
	  int count = commentMapper.insert(comments);
	  if (count>0){
		Blog blog = blogMapper.selectById(comments.getBid());
		blog.setCommentNum(blog.getCommentNum()+1);
		blogMapper.updateById(blog);
		return new ResultVo(0,"comment success");
	  }
	  return new ResultVo(100,"comment fail");
    }

    @Override
    public ResultVo delComments(int cid) {
	  Comments comments = commentMapper.selectById(cid);
	  Blog blog = blogMapper.selectById(comments.getBid());
	  int i = commentMapper.deleteById(cid);
	  if(i>0){
	      blog.setCommentNum(blog.getCommentNum()-1);
	      blogMapper.updateById(blog);
	      return new ResultVo(0,"del success");
	  }
	  return new ResultVo(100,"del fail");
    }
}
